package pio.action.aspirante;
//import DB_OBSOLETA.AspiranteFormDB;
import DBMS.DataBaseManager;
import clases.Aspirante;
import clases.Institucion;
import clases.InstitucionAspirante;
import java.util.ArrayList;
import java.util.List;
//import javax.mail.Session;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

/**
 *
 * @author charles
 */
public class Modificar extends DispatchAction {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";

    public ActionForward populate(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        Aspirante a = (Aspirante) form;
        request.setAttribute("instituciones", Institucion.getAll());
        DataBaseManager dbms = DataBaseManager.getInstance();
        String cedula = String.valueOf(request.getSession().getAttribute("cedula"));
        Aspirante aa = new Aspirante();
        aa.setCedula(cedula);
        ArrayList  <InstitucionAspirante> list = dbms.consultarAspirantes(aa);
        List lis = list.get(0).getAspirantes();
        aa = (Aspirante) lis.get(0);
        a.cloneWith(aa);
        request.setAttribute("Aspirante", a);
        return mapping.findForward(FAILURE);
    }

    public ActionForward update(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        Aspirante aspirante = (Aspirante) form;
        request.setAttribute("instituciones", Institucion.getAll());
        DataBaseManager dbms = DataBaseManager.getInstance();
        if (dbms.modificarAspirante(aspirante)) {
            aspirante.setMensaje("Aspirante modificado con exito");
            return mapping.findForward(SUCCESS);
        } else {
            aspirante.setMensaje("No se pudo modificar el aspirante");
            return mapping.findForward(FAILURE);
        }
    }
}
